package com.github.kotlindemo.activity

import android.os.Environment
import android.view.View
import androidx.camera.core.CameraSelector
import androidx.camera.core.ImageCapture
import androidx.camera.core.ImageCaptureException
import androidx.camera.core.Preview
import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.core.content.ContextCompat
import androidx.lifecycle.LifecycleOwner
import com.github.kotlindemo.R
import com.github.kotlindemo.base.BaseActivity
import com.github.kotlindemo.databinding.ActivityCameraXBinding
import com.google.common.util.concurrent.ListenableFuture
import java.io.File
import java.util.concurrent.Executors

/**
 * 相机预览界面
 */
class CameraXActivity : BaseActivity(), View.OnClickListener {

    private lateinit var binding: ActivityCameraXBinding;
    private lateinit var cameraProviderFuture: ListenableFuture<ProcessCameraProvider>;
    private var defaultCamera = CameraSelector.LENS_FACING_BACK;
    private lateinit var cameraProvider: ProcessCameraProvider;
    private lateinit var imageCapture: ImageCapture;

    override fun getLayoutId(): Int {
        return R.layout.activity_camera_x;
    }

    override fun getView(): View {
        binding = ActivityCameraXBinding.inflate(layoutInflater);
        return binding.root;
    }

    override fun initUI() {
        binding.switchBtnId.setOnClickListener(this);
        binding.takeBtnId.setOnClickListener(this);
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.switch_btn_id -> {
                defaultCamera = if (defaultCamera == CameraSelector.LENS_FACING_BACK) {
                    CameraSelector.LENS_FACING_FRONT;
                } else {
                    CameraSelector.LENS_FACING_BACK;
                }
                cameraProvider.unbindAll();
                bindPreview(cameraProvider);
            }

            R.id.take_btn_id -> {
                val path = getExternalFilesDir(Environment.DIRECTORY_PICTURES).toString() + "/test.jpg";
                val outputFileOptions = ImageCapture.OutputFileOptions.Builder(File(path)).build();
                imageCapture.takePicture(outputFileOptions, Executors.newSingleThreadExecutor(),
                    object : ImageCapture.OnImageSavedCallback {
                        override fun onImageSaved(outputFileResults: ImageCapture.OutputFileResults) {
                            //Toast.makeText(CameraActivity.this, "拍照成功", Toast.LENGTH_SHORT);
//                            ToastUtil.showShort("拍照成功");
                        }

                        override fun onError(exception: ImageCaptureException) {
                            //Toast.makeText(CameraActivity.this, "拍照失败", Toast.LENGTH_SHORT);
//                            ToastUtil.showShort("拍照失败");
                        }
                    });
            }
        }
    }

    override fun bindData() {
        cameraProviderFuture = ProcessCameraProvider.getInstance(this);
        cameraProviderFuture.addListener(Runnable {
            cameraProvider = cameraProviderFuture.get();
            bindPreview(cameraProvider);
        }, ContextCompat.getMainExecutor(this));
    }

    private fun bindPreview(cameraProvider: ProcessCameraProvider) {
        var preview: Preview = Preview.Builder()
            .build();

        var cameraSelector: CameraSelector = CameraSelector.Builder()
            .requireLensFacing(defaultCamera)
            .build();

        preview.setSurfaceProvider(binding.previewView.surfaceProvider);
        imageCapture = ImageCapture.Builder().build();
        var camera = cameraProvider.bindToLifecycle(this as LifecycleOwner, cameraSelector, preview, imageCapture);
    }
}